#include "myframe.h"

MyFrame::MyFrame(QWidget *parent) :
    QFrame(parent)
{
    drawing = false;
    searchIsValid = false;
    image = new QImage("");
}

MyFrame::~MyFrame(){
    delete image;
}

void MyFrame::paintEvent(QPaintEvent *e){
    QFrame::paintEvent(e);
    QPainter painter(this);
    if(!image->isNull()){
        painter.drawImage(imageStartWidth, imageStartHeight, (*image));
        if(drawing){
            painter.setPen(QColor(255, 0, 0));
            painter.drawRect(startX, startY, endX, endY);
        }
        if(searchIsValid){
            painter.setPen(QColor(255, 0, 0));
            for(int i = 0; i < n; ++i){
                painter.drawRect(coord[i][0], coord[i][1], coord[i][2], coord[i][3]);
            }
        }
    }
}

void MyFrame::setDraw(){
    drawing = !drawing;
}

void MyFrame::setPositions(int sX, int sY, int eX, int eY, QLineEdit *w, QLineEdit *h){
    if(!image->isNull()){
        startX = sX;
        startY = sY;
        endX = eX;
        endY = eY;
        if(startSearch()){
            if((startX + endX) < imageStartWidth){
                endX = imageStartWidth - startX;
            }
            if((startY + endY) < imageStartHeight){
                endY = imageStartHeight - startY;
            }
            if((startX + endX) > imageEndWidth){
                endX = imageEndWidth - startX;
            }
            if(startY + endY > imageEndHeight){
                endY = imageEndHeight - startY;
            }
            this->repaint();
            w->setText(QString::number(abs(endX)));
            h->setText(QString::number(abs(endY)));
        }
    }
    else{
        w->setText("");
        h->setText("");
    }
}

void MyFrame::setBackgroundImage(QImage img){
    *image = resizeImage(img);
    if(drawing){
        drawing = false;
        repaint();
        drawing = true;
    }
    else{
        repaint();
    }
}

QImage MyFrame::resizeImage(QImage img){
    if((img.width() > (this->width() - 4)) || (img.height() > (this->height() - 4))){
        int newW = (this->height() * img.width()) / img.height();
        int newH = (this->width() * img.height()) / img.width();
        if(newW > this->width()){
            img = img.scaled(this->width() - 4, newH - 4);
            imageStartWidth = 2;
            imageStartHeight = ((this->height() - newH) >> 1) + 2;
        }
        else{
            img = img.scaled(newW - 4, this->height() - 4);
            imageStartWidth = ((this->width() - newW) >> 1) + 2;
            imageStartHeight = 2;
        }
    }
    else{
        imageStartWidth = ((this->width() - img.width()) >> 1) + 2;
        imageStartHeight = ((this->height() - img.height()) >> 1) + 2;
    }
    imageEndWidth = imageStartWidth + img.width();
    imageEndHeight = imageStartHeight + img.height();
    return img;
}

bool MyFrame::startSearch(){
    return (startX >= imageStartWidth) && (startY >= imageStartHeight) && (startX <= imageEndWidth) && (startY <= imageEndHeight);
}

bool MyFrame::isImage(){
    return !image->isNull();
}

//only big frame
void MyFrame::drawSearchresult(int **coordIn, int nIn){
    searchIsValid = true;
    n = nIn;
    coord = new int*[n];
    for(int i = 0; i < n; ++i){
        coord[i] = new int[4];
        for(int j = 0; j < 4; ++j){
            coord[i][j] = coordIn[i][j];
        }
    }
    repaint();
    delete []coord;
    searchIsValid = false;
}

void MyFrame::removeBackgroundImage(){
    image = new QImage();
    repaint();
    startX = 0;
    startY = 0;
    endX = 0;
    endY = 0;
}

void MyFrame::mouseMoveEvent(QMouseEvent *ev){
    this->x = ev->x();
    this->y = ev->y();
    emit mouseOn();
}

void MyFrame::mousePressEvent(QMouseEvent *ev){
    this->x = ev->x();
    this->y = ev->y();
    emit mousePressed();
}

void MyFrame::mouseReleaseEvent(QMouseEvent *ev){
    this->x = ev->x();
    this->y = ev->y();
    emit mouseExit();
}

void MyFrame::leaveEvent(QEvent *)
{
    emit mouseLeaved();
}

void MyFrame::enterEvent(QEvent *)
{
    emit mouseEntered();
}
